﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace ECCSystem.DataAccess
{ 
    class ClsUserMaintenance:DBObject 
    {
        public ClsUserMaintenance()
        { }

        //Get List
        public DataSet GetList()
        {
            return this.RunProcedure("sp_UserInfo_List", new IDataParameter[] { }, "UserInfo");
        }

        //add user
        public int AddUser(string strLoginID,string strLogonName,string strUserRole,string strUserName,string strOperator)
        {
            int intRowsAffected = 0;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@LoginID",SqlDbType.VarChar ,5),
                new SqlParameter("@LogonName",SqlDbType.VarChar,20),
                new SqlParameter("@UserRole",SqlDbType.VarChar,10),
                new SqlParameter("@UserName",SqlDbType.NVarChar,30),
                new SqlParameter("@Operator",SqlDbType.NVarChar,30)
            };
            parameters[0].Value = strLoginID;
            parameters[1].Value = strLogonName;
            parameters[2].Value = strUserRole;
            parameters[3].Value = strUserName;
            parameters[4].Value = strOperator;
            return this.RunProcedure("sp_UserInfo_Add", parameters, ref intRowsAffected);
        }

        //Edit User
        public int EditUser(long lngID, string strLoginID, string strLogonName, string strUserRole, string strUserName, string strOperator)
        {
            int intRowsAffected = 0;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID",SqlDbType.BigInt),
                new SqlParameter("@LoginID",SqlDbType.VarChar ,5),
                new SqlParameter("@LogonName",SqlDbType.VarChar,20),
                new SqlParameter("@UserRole",SqlDbType.VarChar,10),
                new SqlParameter("@UserName",SqlDbType.NVarChar,30),
                new SqlParameter("@Operator",SqlDbType.NVarChar,30)
            };
            parameters[0].Value = lngID;
            parameters[1].Value = strLoginID;
            parameters[2].Value = strLogonName;
            parameters[3].Value = strUserRole;
            parameters[4].Value = strUserName;
            parameters[5].Value = strOperator;
            return this.RunProcedure("sp_UserInfo_Edit", parameters, ref intRowsAffected);
        }

		//Edit User
		public int EditUser(string strLoginID, string strIP)
		{
			int intRowsAffected = 0;
			SqlParameter[] parameters = new SqlParameter[] {
                new SqlParameter("@LogonName",SqlDbType.VarChar,20),
				new SqlParameter("@IP",SqlDbType.NVarChar,30)
            };
			parameters[0].Value = strLoginID;
			parameters[1].Value = strIP;
			return this.RunProcedure("sp_UserInfo_Edit_IP", parameters, ref intRowsAffected);
		}

        //Delete User
        public int DeleteUser(long lngID, string strOperator)
        {
            int intRowsAffected = 0;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID",SqlDbType.BigInt),
                new SqlParameter("@Operator",SqlDbType.NVarChar,30) };
            parameters[0].Value = lngID;
            parameters[1].Value = strOperator;
            return this.RunProcedure("sp_UserInfo_Delete", parameters, ref intRowsAffected);
        }

        //Check this User if exist or not
        public int CheckUserLoginID(long lngID,string strLoginID)
        {
            int intRowsAffected = 0;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID",SqlDbType.BigInt),
                new SqlParameter("@LoginID",SqlDbType.VarChar ,5)
            };
            parameters[0].Value = lngID;
            parameters[1].Value = strLoginID;
            return this.RunProcedure("sp_UserInfo_CheckLoginID", parameters, ref intRowsAffected);
        }
        public int CheckUserLogonName( string strLogonName)
        {
            int intRowsAffected = 0;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@LogonName",SqlDbType.VarChar,20) };
            parameters[0].Value = strLogonName;
            return this.RunProcedure("sp_UserInfo_CheckLogonName", parameters, ref intRowsAffected);
        }

        public DataSet GetLoginUser(string strLogonName)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@LogonName", SqlDbType.VarChar, 20) };
            parameters[0].Value = strLogonName;
            return this.RunProcedure("sp_UserInfo_CheckLogonName", parameters, "LoginUser");
        }

        public DataSet CheckAdminUser()
        {
            return this.RunProcedure("sp_UserInfo_CheckAdmin", new IDataParameter[] { }, "AdminUser");
        }
    }
}
